<?php namespace Common\Model;
use \Hdphp\Model\Model;
/*
 * 链接模型
 * */	
 	class Link extends Model
 	{
 		protected $table = "link";//操作表
 		protected $validate = array(
			array("lname","required","请填写链接名称",3,3),
		);
		//自动完成
		protected $auto = array(
			array("addtime","time","function",3,1),
			array("logo","thumb_img","method",3,3),
		);
		/*
		 * 缩略图
		 * */
		protected function thumb_img(){
		//编辑时要判断是否有图片改动，如果没有改动路径是隐藏域中的value
			if(isset($_POST["logo"])){
				return $_POST["logo"];
			}
			$path = "Upload/link" . date("ymd");
			$file = Upload::path($path)->type("jpg,png")->size(2000000)->make();
			//p($file);die;
			//判断是否传图，传图将图片进行缩略
			if($file){
				$thumbimg = Image::thumb($file[0]["path"],$path . "/thumb_" . $file[0]['basename'],200,200,5);
				return $thumbimg;
			}else{
				return "";
			}
		}
 		/*
		 * 添加模型
		 * */
 		public function add_link()
		{
			if(!$this->create()) return false;
			$this->add();
			return true;
		}
		/*
		 * 编辑
		 * */
		public function edit($lid)
		{
			//p($_FILES);die;
			if(!$this->create()) return FALSE;
			if(isset($_FILES['logo']) && $_FILES['logo']['error']!=4)
			{
				if($error=Upload::getError()){
					$this->error = $error;
					return FALSE;
				}
			}
			$this->where("lid",$lid)->save();
			return TRUE;
		}
		/*
		 * 删除
		 * */
		public function del($lid)
		{
			$this->where("lid",$lid)->delete();
			return TRUE;
		}
 	}
